package com.tbit.main.service.impl;

import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.log.AccountUserWorkLogDao;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.AccountUserWork;
import com.tbit.main.pojo.AccountUserWorkLogSta;
import com.tbit.main.pojo.GeoreGionBound;
import com.tbit.main.service.*;
import com.tbit.main.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author LMY
 * @create 2021-03-23 15:00
 */
@Service("/accountUserWorkService")
public class AccountUserWorkLogServiceImpl implements AccountUserWorkLogService {

    @Autowired
    private AccountUserWorkLogDao accountUserWorkLogDao;
    @Autowired
    private AccountUserService accountUserService;
    @Autowired
    private CalibrateService calibrateService;
    @Autowired
    private GeoreGionService georeGionService;
    @Autowired
    private RoleService roleService;

    @Override
    public List<AccountUserWorkLogSta> groupByTime(Integer agentId, Integer userType, Integer usermainId, String startTime, String endTime, List<Integer> regionId) {
        List<AccountUserWorkLogSta> dlses;

        /**查询代理商运维账号*/
        List<Integer> accountUserIds = new ArrayList<>();
        /**判断当前账号是不是运维人员*/
        if (roleService.getRoleCode(userType).equals(1)) {
            accountUserIds.add(usermainId);
        }

        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, agentId);
        params.put("userMainIds", StringUtil.getListString(accountUserIds));
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);
        if (regionId != null) {
            List<GeoreGionBound> bounds = georeGionService.getBoundRegionIds(regionId);
            if (bounds != null && !bounds.isEmpty()) {
                List<Integer> userIds = bounds.stream().map(GeoreGionBound::getAccountUserId).collect(Collectors.toList());
                userIds = userIds.stream().filter(Objects::nonNull).collect(Collectors.toList());
                List<AccountUser> accountUsers = accountUserService.getByIds(userIds);
                params.put("phone", StringUtil.getArrayListString(accountUsers.stream().map(AccountUser::getPhone).collect(Collectors.toList())));
            } else {
                params.put("phone", "'notExist'");
            }
        }

        dlses = accountUserWorkLogDao.groupByTime(params);

        List<AccountUserWorkLogSta> dlsefilters = new ArrayList<AccountUserWorkLogSta>();
        if (dlses.size() > 0) {
            for (AccountUserWorkLogSta dls : dlses) {
                AccountUser au = accountUserService.getById(dls.getAccountUserId());
                if (au != null) {
                    dls.setName(au.getName());
                    dls.setPhone(au.getPhone());
                    dlsefilters.add(dls);
                }
            }
        }

        return dlsefilters;

    }

    @Override
    public List<AccountUserWork> getByTime(Integer accountId, Integer userMainId, String startTime, String endTime, Integer mapType) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put("userMainId", userMainId);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);
        List<AccountUserWork> accountUserWorkLogs = accountUserWorkLogDao.getByTime(params);
        if (accountUserWorkLogs.size() > 0) {
            calibrateService.calibrateUserWork(accountUserWorkLogs, mapType);
        }
        return accountUserWorkLogs;
    }

    @Override
    public void insert(AccountUserWork accountUserWork) {
        accountUserWorkLogDao.insert(accountUserWork);
    }
}
